package com.zero.insp.controller.H5;

import com.zero.insp.common.PJson;
import com.zero.insp.common.PPageInfo;
import com.zero.insp.common.PResult;
import com.zero.insp.domain.TCompanyExh;
import com.zero.insp.domain.TCompanyExhExample;
import com.zero.insp.services.InspService;
import com.zero.insp.utils.DateUtil;
import com.zero.insp.views.CompanyAnalView;
import com.zero.insp.views.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author : pengjie
 * @PackageName : com.zero.insp.controller.H5
 * @Description : TODO
 * @email : 627799251@qq.com
 * @Date : 2019/3/27 14:14
 */

@Api(value="H5前端页面请求接口")
@RestController
public class H5InspController {

    @Autowired
    private InspService inspService;

    @ApiOperation("H5前端-每期展区统计数据")
    @ApiImplicitParam(name="period_num",value="期数",dataType="string", paramType = "query",example="一期")
    @RequiresUser
    @GetMapping("/h5/v1/insp/anal")
    public PResult<List<CompanyAnalView>> anal(@RequestParam(name= "period_num",required = true)String period_num) {
        List<CompanyAnalView> result = inspService.companyAnal(period_num);

        return new PResult<>(result);
    }
    @ApiOperation("H5前端-确认巡检")
    @ApiImplicitParam(name="insp_id",value="巡检记录id",dataType="long", paramType = "query")
    @RequiresUser
    @PostMapping("/h5/v1/insp/fixed")
    public PJson fixed(@RequestParam(name= "insp_id",required = true)long insp_id) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        TCompanyExh companyExh = new TCompanyExh();
        companyExh.setInspStatus(2);
        companyExh.setInspUser(user.getNick());
        companyExh.setInspTime(new Date());

        TCompanyExh companyDB = inspService.findByKey(insp_id);

        inspService.edit(companyExh, companyDB.getCompanyName());

        return PJson.succ();
    }

    @ApiOperation("H5前端-选择巡检")
    @ApiImplicitParams({
            @ApiImplicitParam(name="period_num",value="期数",dataType="string", paramType = "query",example="一期"),
            @ApiImplicitParam(name="lib_nums",value="馆号，以逗号隔开",dataType="string", paramType = "query", example = "11.3,9.3"),
            @ApiImplicitParam(name="exh_area",value="展区",dataType="string", paramType = "query",example="电子信息区"),
            @ApiImplicitParam(name="insp_status",value="巡检状态；1未巡检，2已巡检",dataType="int", paramType = "query"),
            @ApiImplicitParam(name="insp_time",value="巡检时间",dataType="string", paramType = "query",example="2019-03-27"),
            @ApiImplicitParam(name="insp_user",value="巡检人姓名",dataType="string", paramType = "query",example = "张三")
    })
    @RequiresUser
    @GetMapping("/h5/v1/insp/sl")
    public PResult<PPageInfo<TCompanyExh>> sl(String period_num, String lib_nums, String exh_area,
                                              @RequestParam(name= "insp_status",defaultValue = "0")int insp_status,
                                              String insp_time, String insp_user,
                                              @RequestParam(name= "page",defaultValue = "1")int page,
                                              @RequestParam(name= "size",defaultValue = "10")int size) {
        TCompanyExhExample example = new TCompanyExhExample();
        TCompanyExhExample.Criteria criteria = example.createCriteria();

        if (period_num != null){
            criteria.andPeriodNumEqualTo(period_num);
        }
        if (insp_time != null){
            Date cur = DateUtil.parseDate(insp_time, DateUtil.DEFAULT_PATTERN);
            Date start = DateUtil.getDayBegin(cur);
            Date end = DateUtil.getDayEnd(cur);
            criteria.andInspTimeBetween(start, end);
        }
        if (exh_area != null){
            criteria.andExhAreaEqualTo(exh_area);
        }
        if (insp_user != null){
            criteria.andInspUserEqualTo(insp_user);
        }
        if (insp_status != 0){
            criteria.andInspStatusEqualTo(insp_status);
        }
        if (lib_nums != null){
            criteria.andLibNumIn(Arrays.asList(lib_nums.split(",")));
        }
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        if (user.getRoles().contains("巡检用户") && insp_status == 2){
            criteria.andInspUserEqualTo(user.getNick());
        }

        example.createCriteria();
        example.setOrderByClause("company_name desc");

        PPageInfo<TCompanyExh> cc = inspService.query(example, page, size);

        return new PResult<>(cc);
    }
    @ApiOperation("H5前端-选择巡检-V2")
    @ApiImplicitParams({
            @ApiImplicitParam(name="period_num",value="期数",dataType="string", paramType = "query",example="一期"),
            @ApiImplicitParam(name="lib_nums",value="馆号，以逗号隔开",dataType="string", paramType = "query", example = "11.3,9.3"),
            @ApiImplicitParam(name="exh_area",value="展区",dataType="string", paramType = "query",example="电子信息区"),
            @ApiImplicitParam(name="insp_status",value="巡检状态；1未巡检，2已巡检",dataType="int", paramType = "query"),
            @ApiImplicitParam(name="insp_stime",value="巡检开始时间",dataType="string", paramType = "query",example="2019-03-27"),
            @ApiImplicitParam(name="insp_etime",value="巡检结束时间",dataType="string", paramType = "query",example="2019-04-27"),
            @ApiImplicitParam(name="insp_user",value="巡检人姓名",dataType="string", paramType = "query",example = "张三")
    })
    @RequiresUser
    @GetMapping("/h5/v2/insp/sl")
    public PResult<PPageInfo<TCompanyExh>> sl2(String period_num, String lib_nums, String exh_area,
                                              @RequestParam(name= "insp_status",defaultValue = "0")int insp_status,
                                              String insp_stime, String insp_etime, String insp_user,
                                              @RequestParam(name= "page",defaultValue = "1")int page,
                                              @RequestParam(name= "size",defaultValue = "10")int size) {
        TCompanyExhExample example = new TCompanyExhExample();
        TCompanyExhExample.Criteria criteria = example.createCriteria();

        if (period_num != null){
            criteria.andPeriodNumEqualTo(period_num);
        }
        if (insp_stime != null && insp_etime != null){
            Date start = DateUtil.getDayBegin(DateUtil.parseDate(insp_stime, DateUtil.DEFAULT_PATTERN));
            Date end = DateUtil.getDayEnd(DateUtil.parseDate(insp_etime, DateUtil.DEFAULT_PATTERN));
            criteria.andInspTimeBetween(start, end);
        }
        if (exh_area != null){
            criteria.andExhAreaEqualTo(exh_area);
        }
        if (insp_user != null){
            criteria.andInspUserEqualTo(insp_user);
        }
        if (insp_status != 0){
            criteria.andInspStatusEqualTo(insp_status);
        }
        if (lib_nums != null){
            criteria.andLibNumIn(Arrays.asList(lib_nums.split(",")));
        }

        example.createCriteria();
        example.setOrderByClause("company_name desc");

        PPageInfo<TCompanyExh> cc = inspService.query(example, page, size);

        return new PResult<>(cc);
    }
    @ApiOperation("H5前端-搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name="period_num",value="期数,如：一期",dataType="string", paramType = "query",example="一期"),
            @ApiImplicitParam(name="lib_nums",value="馆号，以逗号隔开，如：11.3,9.3",dataType="string", paramType = "query", example = "11.3,9.3"),
            @ApiImplicitParam(name="exh_area",value="展区",dataType="string", paramType = "query",example="电子信息区"),
            @ApiImplicitParam(name="company_name",value="企业名称",dataType="string", paramType = "query"),

    })
    @RequiresRoles({"高级用户"})
    @GetMapping("/h5/v1/insp/list")
    public PResult<PPageInfo<TCompanyExh>> list(String company_name, String period_num, String exh_area, String lib_nums,
                                                @RequestParam(name= "page",defaultValue = "1")int page,
                                                @RequestParam(name= "size",defaultValue = "10")int size) {
        TCompanyExhExample example = new TCompanyExhExample();
        TCompanyExhExample.Criteria criteria = example.createCriteria();

        if (period_num != null){
            criteria.andPeriodNumEqualTo(period_num);
        }
        if (exh_area != null){
            criteria.andExhAreaEqualTo(exh_area);
        }
        if (lib_nums != null){
            criteria.andLibNumIn(Arrays.asList(lib_nums.split(",")));
        }

        if (company_name != null){
            criteria.andCompanyNameLike("%"+company_name+"%");
        }

        example.setOrderByClause("company_name desc");

        PPageInfo<TCompanyExh> cc = inspService.query(example, page, size);

        return new PResult<>(cc);
    }
    @ApiOperation("H5前端-游客-每期展区统计数据")
    @ApiImplicitParam(name="period_num",value="期数",dataType="string", paramType = "query",example="一期")
    @RequiresGuest
    @GetMapping("/yk/v1/insp/anal")
    public PResult<List<CompanyAnalView>> ykanal(@RequestParam(name= "period_num",required = true)String period_num) {
        List<CompanyAnalView> result = inspService.companyAnal(period_num);

        return new PResult<>(result);
    }
    @ApiOperation("H5前端-游客-选择巡检")
    @ApiImplicitParams({
            @ApiImplicitParam(name="period_num",value="期数,如：一期",dataType="string", paramType = "query",example="一期"),
            @ApiImplicitParam(name="lib_nums",value="馆号，以逗号隔开，如：11.3,9.3",dataType="string", paramType = "query", example = "11.3,9.3")
    })
    @RequiresGuest
    @GetMapping("/yk/v1/insp/yk")
    public PResult<PPageInfo<TCompanyExh>> yk(String period_num, String lib_nums,
                                              @RequestParam(name= "page",defaultValue = "1")int page,
                                              @RequestParam(name= "size",defaultValue = "10")int size) {
        TCompanyExhExample example = new TCompanyExhExample();
        TCompanyExhExample.Criteria criteria = example.createCriteria();

        if (period_num != null){
            criteria.andPeriodNumEqualTo(period_num);
        }

        if (lib_nums != null){
            criteria.andLibNumIn(Arrays.asList(lib_nums.split(",")));
        }

        criteria.andInspStatusEqualTo(1);

        example.createCriteria();
        example.setOrderByClause("company_name desc");

        PPageInfo<TCompanyExh> cc = inspService.query(example, page, size);

        return new PResult<>(cc);
    }
}
